<template>
    <div class="home-wrapper">
        <project-menu :detail="projectDetail" :pid="projectId"></project-menu>
        <div class="filters-wrapper">
            <div class="title total_title">任务列表
                <router-link v-if="isAdmin && projectDetail.status == 1" :to="{path: '/newtask/' + projectId + '/0'}">
                    <el-button class="fr" type="primary" size="small">新建任务</el-button>
                </router-link>
            </div>
            <el-row type="flex">
                <el-date-picker v-model="taststime" @change="loadTaskList({})" type="date" size="small" placeholder="选择日期">
                </el-date-picker>
            </el-row>
            <el-table :data="tastlistdata" border
                :header-cell-style="{'text-align':'center','background':'#f7f7f7','color':'#666'}"
                style="width: 100%;margin-top: 10px">
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <el-table-column prop="tast_name" label="任务" align="center">
                </el-table-column>
                <el-table-column prop="tast_staff" label="执行人" align="center">
                </el-table-column>
                <el-table-column prop="tast_progress" label="完成进度" align="center">
                </el-table-column>
                <el-table-column prop="provisions_time" label="规定完成时间" align="center">
                </el-table-column>
                <el-table-column prop="update_time" label="更新时间" align="center">
                </el-table-column>
                <el-table-column prop="operation" label="操作" align="center">
                    <template v-if="isAdmin && projectDetail.status == 1" slot-scope="scope">
                        <el-button  @click="copyTask(scope.row)" size="mini">复制</el-button>
                        <el-button @click="modifyTask(scope.row)" style="margin-left: 5px;" size="mini" type="primary">修改</el-button>
                        <el-button @click="delTask(scope.row)" style="margin-left: 5px;" size="mini" type="danger">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-row type="flex" justify="end" style="margin-top: 20px;">
                <el-pagination background layout="prev, pager, next" :current-page.sync="curPage" :total="total" :page-size="pageSize" @current-change="handleCurrentChange($event)">
                </el-pagination>
            </el-row>
        </div>
    </div>
</template>

<script>
import { pageParams,isAdmin } from '@/settings'
    export default {
        data() {
            return {
                projectId: 0,
                projectDetail: {},
                pageSize: pageParams.pageSize,
                curPage: 1,
                total: 0,
                tastlistdata: [],
                taststime: '',
                isAdmin: false
            }
        },
        created() {
            this.projectId = this.$router.currentRoute.params.id
        },
        mounted() {
            this.isAdmin = isAdmin(this.$store.state.user.id_role)
            // this.projectId = this.$router.currentRoute.params.id
            this.loadData()
        },
        methods: {
            loadData() {
                this.$axios.get('/project/detail', {projectid: this.projectId})
                .then(e => {
                    // let data = e.data
                    // console.log('项目详情', e.data)
                    this.projectDetail = e.data
                    if (!this.isAdmin) {
                        this.isAdmin = this.projectDetail.id_manager == this.$store.state.user.uid
                    }
                })
                this.loadTaskList({})
            },
            loadTaskList(param) {
                param['number'] = this.pageSize
                param['projectid'] = this.projectId
                param['keyword'] = this.taststime
                if (!param['page']) {
                    param['page'] = 1
                }
                this.curPage = param['page']
                this.$axios.get('/tast/total/list', param)
                    .then(e => {
                        this.tastlistdata = e.data.items
                        this.total = e.data.total
                        // console.log('任务', e.data)
                    })
            },
            handleCurrentChange(val) {
                this.loadTaskList({page: val})
            },
            copyTask(item) {
              // let param = {id: item.id}
              this.$router.push({
                  path: '/newtask/' + this.projectId + '/0',
                  query: {
                      copy: item.id
                  }
              })
//              this.$axios.post('/task/copy', param)
//                  .then(e => {
//                    this.$message.success(e.msg ? e.msg : '操作成功')
//                    console.log(e)
//                    this.$router.push({
//                        path: '/newtask/' + this.projectId + '/' + e.data.id
//                    })
//                  })
            },
            modifyTask(item) {
              this.$router.push({
                  path: '/newtask/' + this.projectId + '/' + item.id
              })
            },
            delTask(item) {
              this.$confirm('此操作将删除该项, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
              }).then(() => {
                  this.$axios.post('/task/delete', {ids: [item.id]})
                  .then((e) => {
                      this.$message({
                          type: 'success',
                          message: e.msg ? e.msg : '删除成功!'
                      })
                      this.loadTaskList({})
                  })
              }).catch(() => {
              })
            }
        }
    }
</script>
